package com.wh.TestSpinlock;

import java.util.concurrent.TimeUnit;

public class Test {
    public static void main(String[] args) throws InterruptedException {
        //Lock lock = new ReentrantLock();
        //lock.lock();
        //lock.unlock();

        //底层使用的是自旋锁CAS
        SpinlockDemo1 spinlockDemo1 = new SpinlockDemo1();
        new Thread(() -> {
            spinlockDemo1.myLock();
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                spinlockDemo1.myUnLock();
            }
        }, "A").start();
        new Thread(() -> {
            spinlockDemo1.myLock();
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                spinlockDemo1.myUnLock();
            }
        }, "B").start();
    }
}
